#include "Logger.h"
#include <log4cxx/propertyconfigurator.h>
#include <iostream>
#include <fstream>

#include <QtCore/QDir>
#include "defs.h"

namespace jcrada {
    Logger Logger::INSTANCE;

    Logger::Logger() {
#ifdef MACOSX
        std::string filename( DEF_RESOURCES + "log4cxx/MacOSX.properties");
#elif UNIX
        std::string filename(DEF_RESOURCES + "log4cxx/Unix.properties");
#else 
        //WIN32
        std::string filename(DEF_RESOURCES + "log4cxx/Win32.properties");
#endif
        std::ifstream file(filename.c_str());
        if (!file.fail()) {
            log4cxx::PropertyConfigurator::configure(filename);
            _verbose = log4cxx::Logger::getLogger("verbose");
            _plain = log4cxx::Logger::getLogger("plain");
        } else {
            std::cout << QDir::currentPath().toStdString() << std::endl;
            std::cout << "Error: File " << filename << " does not exist" << std::endl;
            exit(1);
            //
            //            _file_appender = new log4cxx::FileAppender(log4cxx::LayoutPtr(new log4cxx::SimpleLayout()),
            //                    "../logs/temp.log", true);
            //            log4cxx::helpers::Pool p;
            //            _file_appender->activateOptions(p);
            //            log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(_file_appender));
            //            log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug());
        }
    }

    //    Logger::Logger() {
    //        try {
    //            log4cxx::PropertyConfigurator::configure("/Resources/log4cxx.properties");
    //        }catch (std::exception& ex) {
    //            std::cout << "Error: Loading MScThesis.app/Contents/Resources/log4cxx.properties" << std::endl;
    //            std::cout << "Configuring standard log4cxx for MScThesis.app" << std::endl;
    //
    //            _file_appender = new log4cxx::FileAppender(log4cxx::LayoutPtr(new log4cxx::SimpleLayout()),
    //                    "MScThesis.log", true);
    //            log4cxx::helpers::Pool p;
    //            _file_appender->activateOptions(p);
    //            log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(_file_appender));
    //            log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug());
    //        }
    //        _logger = log4cxx::Logger::getLogger("MScThesis");
    //    }

    Logger::~Logger() {
        //        delete _file_appender;
        //        if (_verbose != NULL) {
        //            delete this->_verbose;
        //        }
        //        if (_file_appender != NULL) {
        //            delete this->_file_appender;
        //        }
    }

    Logger& Logger::instance() {
        return INSTANCE;
    }

    log4cxx::LoggerPtr& Logger::verboseLogger() {
        return _verbose;
    }

    log4cxx::LoggerPtr& Logger::plainLogger() {
        return _plain;
    }

    log4cxx::LoggerPtr& Logger::PLAIN() {
        return instance().plainLogger();
    }

    log4cxx::LoggerPtr& Logger::VERBOSE() {
        return instance().verboseLogger();
    }

    void Logger::test() {
        LOG4CXX_DEBUG(jcrada::Logger::VERBOSE(), "Esto es qu\u00e9?");
        LOG4CXX_ERROR(jcrada::Logger::VERBOSE(), "Esto es qu\u00e9?");
        LOG4CXX_FATAL(jcrada::Logger::VERBOSE(), "Esto es qu\u00e9?");
        LOG4CXX_INFO(jcrada::Logger::VERBOSE(), "Esto es qu\u00e9?");
        LOG4CXX_WARN(jcrada::Logger::VERBOSE(), "Esto es qu\u00e9?");

        LOG4CXX_DEBUG(jcrada::Logger::PLAIN(), "Esto es qu\u00e9?");
        LOG4CXX_ERROR(jcrada::Logger::PLAIN(), "Esto es qu\u00e9?");
        LOG4CXX_FATAL(jcrada::Logger::PLAIN(), "Esto es qu\u00e9?");
        LOG4CXX_INFO(jcrada::Logger::PLAIN(), "Esto es qu\u00e9?");
        LOG4CXX_WARN(jcrada::Logger::PLAIN(), "Esto es qu\u00e9?");

    }
}
